package restaurant.selector.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AddRestaurantShell extends Dialog {

	private String name;
	private boolean quick;
	private int distance;
	
	private Text nameText, distText;
	private Button quickButton;
	
	public AddRestaurantShell(Shell shell ){
		this(shell, SWT.NONE);
	}
	
	public AddRestaurantShell(Shell shell, int style ){
		
		super( shell, style );
	}

	public void open () {
        Shell parent = getParent();
        final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText("Add New Restaurant");

        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        shell.setLayout(gridLayout);
        
        GridData data = new GridData();
		
        Label nameLabel = new Label(shell, SWT.NONE);
        nameLabel.setText("Name :");
        data.widthHint = 100;
        nameLabel.setLayoutData(data);
		
        nameText = new Text(shell, SWT.BORDER);
        data = new GridData();
        data.widthHint = 200;
        nameText.setLayoutData(data);
       
        Label quickLabel = new Label(shell, SWT.NONE);
        data = new GridData();
        data.widthHint = 200;
        quickLabel.setLayoutData(data);
        
        quickButton = new Button(shell, SWT.CHECK);
        quickButton.setText("Quick");
        data = new GridData();
        data.widthHint = 200;
        quickButton.setLayoutData(data);
        
        Label distLabel = new Label(shell, SWT.NONE);
        distLabel.setText("Distance :");
        data = new GridData();
        data.widthHint = 200;
        distLabel.setLayoutData(data);
        
        distText = new Text(shell, SWT.BORDER);
        data = new GridData();
        data.widthHint = 200;
        distText.setLayoutData(data);
        
        Composite buttons = new Composite(shell, SWT.NONE);
        GridLayout buttonLayout = new GridLayout();
        buttonLayout.numColumns = 2;
        buttons.setLayout(buttonLayout);
        
		Button ok = new Button(buttons, SWT.PUSH);
		ok.setText("OK");
		ok.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				name = nameText.getText();
				quick = quickButton.getSelection();
				distance = Integer.parseInt(distText.getText());
				shell.close();
			}
		});
		
		Button cancel = new Button(buttons, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				shell.close();
			}
		});
        
		shell.pack();
        shell.open();
        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) display.sleep();
        }
	}

	public String getName() {
		return name;
	}

	public boolean isQuick() {
		return quick;
	}

	public int getDistance() {
		return distance;
	}
}
